//
//  IndexInfoCell.swift
//  LuYao
//
//  Created by vase on 2022/3/1.
//

import UIKit

class IndexInfoCell: UITableViewCell {
    /** 基准视图*/
    var baseView : UIView = UIView.init()
    //地点label
    var locationLabel : UILabel = UILabel.init()
    //设备数量Label
    var devicesLabel : UILabel = UILabel.init()
    //信息显示
    var infoView : UIView = UIView.init()
    //调节窗帘的拖动条
    var adjustSlider : UISlider = UISlider.init()
    //展示的具体信息
    var info : IndexInfoModel = IndexInfoModel.init()
    
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.backgroundColor = UIColor.clear
        /** 基准视图*/
        baseView.frame = CGRect.init(x: 0, y: 0, width: screenWidth-30, height: 120)
        baseView.layer.masksToBounds = true
        baseView.layer.cornerRadius = 20
        baseView.backgroundColor = UIColor.init(red: 0.63, green: 0.61, blue: 0.57, alpha: 0.95)
        self.addSubview(baseView)
        //地点Label
        locationLabel.text = "卧室"
        locationLabel.font = UIFont.boldSystemFont(ofSize: 16)
        locationLabel.sizeToFit()
        locationLabel.frame.origin = CGPoint.init(x: 15, y: 15)
        baseView.addSubview(locationLabel)
        //设备数量Label
        devicesLabel.text = "2个设备"
        devicesLabel.font = UIFont.systemFont(ofSize: 14)
        devicesLabel.sizeToFit()
        devicesLabel.frame.origin = CGPoint.init(x: 15, y: locationLabel.frame.maxY+5)
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        locationLabel.text = info.location
        locationLabel.sizeToFit() //长度自适应
        devicesLabel.text = "\(info.optionNames.count)个设备"
        if info.optionNames.count > 0 {
            devicesLabel.sizeToFit() //长度自适应
            baseView.addSubview(devicesLabel)
        }
        //信息展示
        var fixY : CGFloat = devicesLabel.frame.maxY+5
        for optionName in info.optionNames {
            let optionImage = UIImageView.init(frame: CGRect.init(x: 15, y: fixY, width: 24, height: 24))
            optionImage.image = UIImage.init(named: optionName)
            self.baseView.addSubview(optionImage)
            let optionLabel = UILabel.init()
            optionLabel.text = info.optionInfos[optionName]
            optionLabel.font = UIFont.systemFont(ofSize: 14)
            optionLabel.sizeToFit()
            optionLabel.frame.origin = CGPoint.init(x: optionImage.frame.maxX+5, y: fixY+5)
            self.baseView.addSubview(optionLabel)
            fixY = fixY + 24 + 5
        }
        //如果需要的话，把拖动条搞出来
        if info.curtain != -1 {
            //调节窗帘的拖动条
            adjustSlider.frame = CGRect.init(x: 30, y: devicesLabel.frame.maxY+5, width: self.frame.width-60, height: 40)
            adjustSlider.minimumValue = 0 //最小值
            //最小值旁边的图片
            adjustSlider.minimumValueImage = UIImage.init(named: "weak")
            adjustSlider.maximumValue = 100 //最大值
            //最大值旁边的图片
            adjustSlider.maximumValueImage = UIImage.init(named: "brightness")
            adjustSlider.minimumTrackTintColor = UIColor.init(red: 0.09, green: 0.65, blue: 0.34, alpha: 1)
            adjustSlider.value = Float(info.curtain)
            adjustSlider.addTarget(self, action: #selector(updateValueOf(sender:)), for: .valueChanged)
            self.addSubview(adjustSlider)
        }
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        // Configure the view for the selected state
    }

    func setInfo(indexInfo : IndexInfoModel) {
        info = indexInfo
        setNeedsLayout()
    }
    
    @objc func updateValueOf(sender : UISlider) {
        if sender.state == .selected {
            
        }
    }
}
